<template>
	<view class="add-address">
		<view class="from">
			<view class="input">
				<text >姓名:</text>
				<input type="text" placeholder="请输入收件人姓名" name='name' v-model="name">
			</view>
			<view class="input">
				<text>电话：</text>
				<input type="text" placeholder="请输入收件人电话" name='phone' v-model="tel" @blur="phone">
						<uni-icons  :type="s6" :color="c6" :size="28" />
			</view>
			<view class="input">
				<text>地区：</text>
				<AddressPicker @change="change" :level="3" name='address'>{{planLocate}} </AddressPicker>
			</view>
			<view class="input">
				<text>详细地址：</text>
				<input type="text" placeholder="街道门牌,楼层房间号等信息" name='xqaddress' v-model="address">
			</view>
			<view class="input">
				<text>邮政编码：</text>
				<input type="text" placeholder="邮政编码" name='code'    v-model="tell" @blur="tells">
				<uni-icons  :type="s5" :color="c5" :size="28" />
			</view>
			<view class="input">
				<text>设为默认收货地址</text>
				<switch @change="Default" color="#a397fd" style="transform:scale(0.7)" :checked='checked' name='default'>
				</switch>
			</view>
		</view>
		<!-- <view class="btn"> -->
			<button @click="gowhere()" class="btn">保存</button>
		<!-- </view> -->
	</view>
</template>

<script>
	import {shczcha, ssdztj, ssdztj2} from '@/service/index.js'
	import AddressPicker from '../../components/lingdang-AddressPicker/AddressPicker.vue'
	import AllAddress from '@/components/lingdang-AddressPicker/AddressData.js'

	export default {
		components: {
			AddressPicker
		},
		data() {
			return {
				planLocate: '点击选择地址',
				checked: true,
				s6:'checkbox-filled',
				s5:'checkbox-filled',
				c6:'transparent',
				c5:'transparent',
				t6:'',
				t5:'',
				name:"",
				tel:"",
				address:'',
				tell:'',
				province:'',
				county:'',
				city:'',
				pidd:3,//收获地址id
				cidd:''	
			}
		},
	 async	onLoad() {
			let res=await shczcha(this.$uid)
			this.pidd=res.result[0].pid
			console.log(this.pidd)
		},
		methods: { //父组件的获取子组件传递的值， 在为josn 有code和name
			change(result) {
				// console.log(JSON.stringify(result.data)); 
				this.planLocate = '';
				result.data.forEach((item, index) => {
					if (index === 0) {
						this.planLocate += item.name
					} else {
						this.planLocate += '-' + item.name
					}
				});
				let arr = this.planLocate.split('-');
				console.log(arr)
				this.province=arr[0]
				this.county=arr[1]
				this.city=arr[2]
			},
			Default(e) {
				this.checked = e.detail.value //true false
				console.log(e.detail.value)
				if(e.detail.value){
					this.cidd=1
				}else{
					this.cidd=2
					this.pidd=this.pidd+2
					console.log(this.pidd)
				}

			},
	
		async gowhere(){
			if(this.t6 && this.t5){
				 if(this.cidd===1){
					 let res=await ssdztj(this.$uid,this.address,this.name,this.tel,this.province,this.county,this.city,1,this.tell)
					if(res.code==200){
						uni.showToast({
							title:'添加成功'
						})
					}else {
						uni.showToast({
							title:'默认收货物地址已存在'
						})
					}					 
				 }	
					  
					  else {
			  console.log(this.$uid,this.address,this.name,this.tel,this.province,this.county,this.city,this.pidd,this.tell)
					  let res=await ssdztj2(this.$uid,this.address,this.name,this.tel,this.province,this.county,this.city,this.pidd,this.tell)
					
					  if(res.code==200){
					  	uni.showToast({
					  		title:'添加成功'
					  	})
					  }else {
					  	uni.showToast({
					  		title:'收货物地址添加失败'
					  	})
					  }	
					  	
					  }
			}
	
			},
			phone(e){
				let phone=e.detail.value
				let exp =/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
				      if (exp.test(phone)) {
				        console.log("验证通过");
				       this.c6 ="green" ;
					    this.t6=true
						this.ph=phone
				      } else {
				       this.c6 = "red";
				       this.s6="clear"
					     this.t6=false
				       
				      }
			},
			tells(e){
				let tell=e.detail.value
				let exp=/^[0-8][0-7]\d{4}$/
				if (exp.test(tell)) {
				  console.log("验证通过");
				 this.c5 ="green" ;
				  this.t5=true
			     this.tell=tell
				 console.log(this.tell)
				
				} else {
				 this.c5 = "red";
				 this.s5="clear"
				   this.t5=false
				 
				}
				
				
			}
		}
	}
</script>

<style lang="scss">
	.add-address {

		.from {
			padding: 5rpx 20px;

			.input {
				width: 100%;
				display: flex;
				align-items: center;
				margin: 10px 0;

				input {
					padding: 10rpx;
					flex: 1;
					display: inline-block;
					border-bottom: 1px solid #e8e8e8;
				}
			}
		}

		.btn {
			width: 90%;
			border-radius: 50rpx;
			background-color: rgb(163, 151, 253);
			color: white;
			margin: 0 auto;
		}
	}
</style>
